import { GameConfig } from "./GameConfig";
import GameManager from "./GameManager";

const { ccclass, property } = cc._decorator;
@ccclass
export default class AudioManager extends cc.Component {

    public SoundType = cc.Enum({
        ST_BGMusic: 1,  //背景音乐
        ST_CommonBtn: 2,
        ST_BombSound: 3,
        ST_PlaceSuccess: 4,
        ST_PlaceFail: 5,
        ST_RotateHexagon: 6,
        ST_SynthesiT1: 7,
        ST_SynthesiT2: 8,
        ST_SynthesiT3: 9,
        ST_Perfact: 10,
    });

    //声音文件路径
    public SoundFile = [
        "",
        "bg3",
        "btnclick",
        "bomb",
        "place_success",
        "place_fail",
        "rotate",
        "synthesi",
        "synthesi2",
        "synthesi3",
        "PERFECT_1"
    ];

    constructor(){
        super();
        GameConfig.SoundSwitch = true;
    }

    public isOpenSound: boolean;
    public curBgm: number;

    //播放音效
    PlaySoundEffect(type) {
        var soundFile = this.SoundFile[type];
        if (soundFile) {
            this.playEffect(soundFile);
        }
    }

    //播放音效
    PlaySoundEffect1(soundFile) {
        this.playEffect(soundFile);
    }

    //通用按钮音效
    PlayBtnSound() {
        this.PlaySoundEffect(this.SoundType.ST_CommonBtn);
    }

    //播放合成音效
    PlaySynthesiSound(count) {
        var type = null;
        if (count == 1) {
            type = this.SoundType.ST_SynthesiT1;
        } else if (count == 2) {
            type = this.SoundType.ST_SynthesiT2;
        } else if (count >= 3) {
            type = this.SoundType.ST_SynthesiT3;
        }
        this.PlaySoundEffect(type);
    }

    //停止-背景-音乐
    StopBackgroundSound() {
        if (this.curBgm != null) {
            cc.audioEngine.stop(this.curBgm);
            this.curBgm = null;
        }
    }

    //暂停-背景-音乐
    PauseBackgroundSound() {
        if (this.curBgm != null)
            cc.audioEngine.pause(this.curBgm);
    }

    //恢复-背景-音乐
    ResumeBackgroundSound() {
        if (this.curBgm != null){
            cc.audioEngine.resume(this.curBgm);
        }else{
            this.playBackgroundMusic();
        }
           
    }

    //打开声音
    OpenSound() {
        GameManager.getInstance().gameData.setSoundSwitch(true);
        GameConfig.SoundSwitch = true;
        this.ResumeBackgroundSound();
    }

    //关闭声音
    CloseSound() {
        GameManager.getInstance().gameData.setSoundSwitch(false);
        GameConfig.SoundSwitch = false;
        this.PauseBackgroundSound();
    }

    playEffect(audio) {
        if (!GameConfig.SoundSwitch)
            return;
        if (!audio) {
            return;
        }

        var url = "sound/" + audio;

        cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
            var audioID = cc.audioEngine.play(clip, false, 1);
        });
    }

    playBackgroundMusic() {
        let audio = this.SoundFile[this.SoundType.ST_BGMusic];
        if (!GameConfig.SoundSwitch)
            return;
        if (!GameConfig.SoundSwitch)
            return null;
        if (!audio) {
            return;
        }

        var self = this;
        if(self.curBgm && cc.audioEngine.getState(this.curBgm) == cc.audioEngine.AudioState.PLAYING){
            return;
        }
        var url = "sound/" + audio;

        cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
            self.curBgm = cc.audioEngine.play(clip, true, 1);
        });
    }
};
